
package com.sbdcpn.utils.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.sbdcpn.interfaces.EntityBean;
import com.sbdcpn.pool.ConnectionPool;
import com.sbdcpn.utils.Log;
import com.sbdcpn.utils.factory.FactoryUtil;
import com.sbdcpn.utils.tools.CommonFun;

public class BaseDAO
{
	public static boolean SHOW_SQL = false;
	
    private BaseDAO(String astrDefaultDataSource)
    {
        _defaultDataSource = astrDefaultDataSource;
    }

    public static void init(String astrDefaultDataSource)
    {
        _defaultDataSource = astrDefaultDataSource;
    }

    public static BaseDAO getInstance()
    {
        return new BaseDAO(_defaultDataSource);
    }

    private List getEmptyObjectByType(String astrType)
    {
        List obj = null;
        if(astrType.equals(ONE_ROW))
            obj = FactoryUtil.newArray();
        else
        if(astrType.equals(COLUMN_NAME))
            obj = FactoryUtil.newArray();
        else
        if(astrType.equals(MUTIL_ROW))
            obj = FactoryUtil.newArray();
        return obj;
    }

    private List getResultSetByType(ResultSet ars, String astrType)
        throws SQLException
    {
        List ret = null;
        if(astrType.equals(ONE_ROW))
            ret = DAOUtil.RsetToArray(ars);
        else
        if(astrType.equals(COLUMN_NAME))
            ret = DAOUtil.getrsToMata(ars);
        else
        if(astrType.equals(MUTIL_ROW))
            ret = DAOUtil.RsetToArray(ars);
        return ret;
    }

    private void getSQLException(Connection acon, SQLException aex)
    {
        try
        {
            if(!acon.isClosed())
                acon.close();
            _log.showLog(aex.toString());
        }
        catch(SQLException e)
        {
            acon = null;
            e.printStackTrace();
        }
        catch(Exception e)
        {
            acon = null;
            e.printStackTrace();
        }
    }

    public Map queryOneRow(String astrSQL, String astrParam[], String astrDataSource)
    {
        if(astrSQL == null || astrSQL.length() == 0)
            return null;
        if(astrParam == null)
        {
            _log.showLog("queryOneRow(): \u65E0\u6548\u7684\u53C2\u6570\u3002");
            return null;
        }
        if(astrParam[0] == null || astrParam[1] == null)
        {
            _log.showLog("queryOneRow(): \u65E0\u6548\u7684\u53C2\u6570\u3002");
            return null;
        }
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        long curTime = System.currentTimeMillis();
        Map ret = FactoryUtil.newMap();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDataSource);
            if(con == null)
            {
                _log.showLog("connection is null");
                Map map = null;
                return map;
            }
            con.setAutoCommit(true);
            
            ps = con.prepareStatement(astrSQL);
            ps = DAOUtil.setParameters(astrParam[0].split(";"), astrParam[1].split(";"), ps);
            if(ps == null)
            {
                con.close();
                con = null;
                Map map1 = null;
                return map1;
            }
            rs = ps.executeQuery();
            List lstmp = DAOUtil.RsetToArray(rs);
            if(lstmp == null || lstmp.isEmpty())
                ret = null;
            else
                ret = (Map)lstmp.get(0);
            rs.close();
            rs = null;
            ps.close();
            ps = null;
            con.close();
            con = null;
            _log.showLog("BaseDAO Query Time = " + (System.currentTimeMillis() - curTime), "debug");
            if (SHOW_SQL) {
            	_log.showLog("...........sql=" + astrSQL);
            	_log.showLog("...........sql value=" + astrParam[0]);
            }
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            System.out.println("SQLException: param[0] = " + astrParam[0]);
            System.out.println("SQLException: param[1] = " + astrParam[1]);
            e.printStackTrace();
            Map map2 = null;
            return map2;
        }
        catch(Exception e)
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException ex)
                {
                    Map map3 = null;
                    return map3;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    Map map4 = null;
                    return map4;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException ex)
                {
                    Map map5 = null;
                    return map5;
                }
                con = null;
            }
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            System.out.println("SQLException: param[0] = " + astrParam[0]);
            System.out.println("SQLException: param[1] = " + astrParam[1]);
            e.printStackTrace();
            Map map6 = null;
            return map6;
        }
        finally
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                con = null;
            }
        }
        return ret;
    }

    public Map queryOneRow(String astrSQL, String astrParam[])
    {
        return queryOneRow(astrSQL, astrParam, _defaultDataSource);
    }

    public Map queryOneRow(String astrSQL, String astrDataSource)
    {
        if(astrSQL == null || astrSQL.length() == 0)
            return FactoryUtil.newMap();
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        Map ret = FactoryUtil.newMap();
        long curTime = System.currentTimeMillis();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDataSource);
            if(con == null)
            {
                _log.showLog("connection is null");
                Map map = null;
                return map;
            }
            con.setAutoCommit(true);
            ps = con.prepareStatement(astrSQL);
            if(ps == null)
            {
                con.close();
                con = null;
                Map map1 = null;
                return map1;
            }
            rs = ps.executeQuery();
            List lstmp = DAOUtil.RsetToArray(rs);
            if(lstmp == null || lstmp.isEmpty())
                ret = null;
            else
                ret = (Map)lstmp.get(0);
            rs.close();
            rs = null;
            ps.close();
            ps = null;
            con.close();
            con = null;
            _log.showLog("queryOneRow(): Query Time = " + (System.currentTimeMillis() - curTime), "debug");
            if (SHOW_SQL) {
            	_log.showLog("...........sql=" + astrSQL);
            }
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            e.printStackTrace();
            Map map2 = null;
            return map2;
        }
        catch(Exception e)
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException ex)
                {
                    Map map3 = null;
                    return map3;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    Map map4 = null;
                    return map4;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException ex)
                {
                    Map map5 = null;
                    return map5;
                }
                con = null;
            }
            System.out.println("Exception: Excute SQL = " + astrSQL);
            e.printStackTrace();
            Map map6 = null;
            return map6;
        }
        finally
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                con = null;
            }
        }
        return ret;
    }

    public Map queryOneRow(String astrSQL)
    {
        return queryOneRow(astrSQL, _defaultDataSource);
    }

    public List query(String astrSQL, String astrDataSource)
    {
        if(astrSQL == null || astrSQL.length() == 0)
            return getEmptyObjectByType(MUTIL_ROW);
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        List ret = getEmptyObjectByType(MUTIL_ROW);
        long curTime = System.currentTimeMillis();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDataSource);
            if(con == null)
            {
                _log.showLog("connection is null");
                List list = getEmptyObjectByType(MUTIL_ROW);
                return list;
            }
            con.setAutoCommit(true);
            ps = con.prepareStatement(astrSQL);
            if(ps == null)
            {
                System.out.println("BaseDAO:query: ps is null " + astrSQL);
                con.close();
                con = null;
                List list1 = getEmptyObjectByType(MUTIL_ROW);
                return list1;
            }
            rs = ps.executeQuery();
            ret = getResultSetByType(rs, MUTIL_ROW);
            rs.close();
            rs = null;
            ps.close();
            ps = null;
            con.close();
            con = null;
            _log.showLog("query(): Query Time = " + (System.currentTimeMillis() - curTime), "debug");
            if (SHOW_SQL) {
            	_log.showLog("...........sql=" + astrSQL);
            }
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            e.printStackTrace();
            List list2 = getEmptyObjectByType(MUTIL_ROW);
            return list2;
        }
        catch(Exception e)
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException ex)
                {
                    List list3 = getEmptyObjectByType(MUTIL_ROW);
                    return list3;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    List list4 = getEmptyObjectByType(MUTIL_ROW);
                    return list4;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException ex)
                {
                    List list5 = getEmptyObjectByType(MUTIL_ROW);
                    return list5;
                }
                con = null;
            }
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            e.printStackTrace();
            List list6 = getEmptyObjectByType(MUTIL_ROW);
            return list6;
        }
        finally
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                con = null;
            }
        }
        return ret;
    }

    public List query(String astrSQL)
    {
        return query(astrSQL, _defaultDataSource);
    }

    public List query(String astrSQL, String astrParams[], String astrDefalutSource)
    {
        List obj = FactoryUtil.newArray();
        String strLogHead = "";
        if(astrSQL == null || astrSQL.length() < 10)
        {
            _log.showLog(strLogHead + "\u65E0\u6548\u7684\u67E5\u8BE2\u8BED\u53E5\u3002");
            return getEmptyObjectByType(MUTIL_ROW);
        }
        if(astrParams == null)
        {
            _log.showLog(strLogHead + "\u65E0\u6548\u7684\u53C2\u6570\u3002");
            return getEmptyObjectByType(MUTIL_ROW);
        }
        if(astrParams[0] == null || astrParams[1] == null)
        {
            _log.showLog(strLogHead + "\u65E0\u6548\u7684\u53C2\u6570\u3002");
            return getEmptyObjectByType(MUTIL_ROW);
        }
        if(astrParams.length < 2)
        {
            _log.showLog(strLogHead + "BaseDAO.query()");
            return getEmptyObjectByType(MUTIL_ROW);
        }
        if(astrDefalutSource == null || astrDefalutSource.equals(""))
            astrDefalutSource = _defaultDataSource;
        String strVal[] = astrParams[0].split(";");
        String strType[] = astrParams[1].split(";");
        long curTime = System.currentTimeMillis();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDefalutSource);
            if(con == null)
            {
                _log.showLog("connection is null.");
                List list = getEmptyObjectByType(MUTIL_ROW);
                return list;
            }
            con.setAutoCommit(true);
            ps = con.prepareStatement(astrSQL);
            ps = DAOUtil.setParameters(strVal, strType, ps);
            if(ps == null)
            {
                System.out.println("BaseDAO:query: ps is null " + astrSQL);
                con.close();
                con = null;
                List list1 = obj;
                return list1;
            }
            rs = ps.executeQuery();
            obj = getResultSetByType(rs, MUTIL_ROW);
            rs.close();
            rs = null;
            ps.close();
            ps = null;
            con.close();
            con = null;
            _log.showLog("query(): Query Time = " + (System.currentTimeMillis() - curTime), "debug");
            if (SHOW_SQL) {
            	_log.showLog("...........sql=" + astrSQL);
            	_log.showLog("...........sql value=" + astrParams[0]);
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            System.out.println("SQLException: param[0] = " + astrParams[0]);
            System.out.println("SQLException: param[1] = " + astrParams[1]);
            List list2 = getEmptyObjectByType(MUTIL_ROW);
            return list2;
        }
        catch(Exception e)
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException ex)
                {
                    List list3 = getEmptyObjectByType(MUTIL_ROW);
                    return list3;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    List list4 = getEmptyObjectByType(MUTIL_ROW);
                    return list4;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException ex)
                {
                    List list5 = getEmptyObjectByType(MUTIL_ROW);
                    return list5;
                }
                con = null;
            }
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            System.out.println("SQLException: param[0] = " + astrParams[0]);
            System.out.println("SQLException: param[1] = " + astrParams[1]);
            e.printStackTrace();
            List list6 = getEmptyObjectByType(MUTIL_ROW);
            return list6;
        }
        finally
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                con = null;
            }
        }
        return obj;
    }

    public List query(String astrSQL, String astrParams[])
    {
        return query(astrSQL, astrParams, _defaultDataSource);
    }

    public List query(String astrSQL[], EntityBean aEntityBean, String astrDefaultSource)
    {
        List obj = FactoryUtil.newArray();
        String strLogHead = new String("BaseDAO.query(EntityBean): ");
        if(astrSQL == null || astrSQL.length == 0)
        {
            _log.showLog(strLogHead + "\u65E0\u6548\u7684\u67E5\u8BE2\u8BED\u53E5\u3002");
            return getEmptyObjectByType(MUTIL_ROW);
        }
        if(aEntityBean == null)
        {
            _log.showLog(strLogHead + "BaseDAO.query()");
            return getEmptyObjectByType(MUTIL_ROW);
        }
        if(astrDefaultSource == null && astrDefaultSource.equals(""))
            astrDefaultSource = _defaultDataSource;
        long curTime = System.currentTimeMillis();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDefaultSource);
            if(con == null)
            {
                _log.showLog("connection is null.");
                List list = getEmptyObjectByType(MUTIL_ROW);
                return list;
            }
            con.setAutoCommit(true);
            ps = con.prepareStatement(astrSQL[0]);
            ps = DAOUtil.setPs(con, astrSQL, aEntityBean);
            if(ps == null)
            {
                con.close();
                List list1 = obj;
                return list1;
            }
            rs = ps.executeQuery();
            obj = getResultSetByType(rs, MUTIL_ROW);
            rs.close();
            rs = null;
            ps.close();
            ps = null;
            con.close();
            con = null;
            _log.showLog("query(): Query Time = " + (System.currentTimeMillis() - curTime), "debug");
            if (SHOW_SQL) {
            	_log.showLog("...........sql=" + astrSQL[0]);
            	_log.showLog("...........sql value=" + astrSQL[1]);
            }
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL[0] = " + astrSQL[0]);
            System.out.println("SQLException: Excute SQL[1] = " + astrSQL[1]);
            System.out.println("SQLException: Excute SQL[2] = " + astrSQL[2]);
            e.printStackTrace();
            List list2 = getEmptyObjectByType(MUTIL_ROW);
            return list2;
        }
        catch(Exception e)
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException ex)
                {
                    List list3 = getEmptyObjectByType(MUTIL_ROW);
                    return list3;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    List list4 = getEmptyObjectByType(MUTIL_ROW);
                    return list4;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException ex)
                {
                    List list5 = getEmptyObjectByType(MUTIL_ROW);
                    return list5;
                }
                con = null;
            }
            System.out.println("SQLException: Excute SQL[0] = " + astrSQL[0]);
            System.out.println("SQLException: Excute SQL[1] = " + astrSQL[1]);
            System.out.println("SQLException: Excute SQL[2] = " + astrSQL[2]);
            e.printStackTrace();
            List list6 = getEmptyObjectByType(MUTIL_ROW);
            return list6;
        }
        finally
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                con = null;
            }
        }
        return obj;
    }

    public List query(String astrSQL[], EntityBean aEntityBean)
    {
        return query(astrSQL, aEntityBean, _defaultDataSource);
    }

    public boolean update(String astrSQL, String astrDataSource)
    {
        boolean ret = false;
        String strLogHead = "BaseDAO.Update(): ";
        if(astrSQL.equalsIgnoreCase(""))
            return ret;
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        long curTime = System.currentTimeMillis();
        Connection con = null;
        PreparedStatement ps = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDataSource);
            if(con == null || con.isClosed())
            {
                boolean flag = ret;
                return flag;
            }
            ps = con.prepareStatement(astrSQL);
            if(ps.executeUpdate() >= 0)
            {
                con.commit();
                ret = true;
            } else
            {
                con.rollback();
            }
            ps.close();
            ps = null;
            con.close();
            con = null;
            _log.showLog("update(): Query Time = " + (System.currentTimeMillis() - curTime), "debug");
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            e.printStackTrace();
            return false;
        }
        catch(Exception e)
        {
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    return false;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.rollback();
                    con.close();
                }
                catch(SQLException ex)
                {
                    return false;
                }
                con = null;
            }
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            e.printStackTrace();
            return false;
        }
        finally
        {
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return false;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.rollback();
                    con.close();
                }
                catch(SQLException e)
                {
                    return false;
                }
                con = null;
            }
        }
        return ret;
    }

    public boolean update(String astrSQL)
    {
        return update(astrSQL, _defaultDataSource);
    }

    public boolean update(String astrSQL, String astrParams[], String astrDataSource)
    {
        boolean ret = false;
        if(astrParams.length != 2)
        {
            _log.showLog("BaseDAO.update(string, string[]):\u4F20\u5165\u7684\u6570\u7EC4\u4E0D\u591A\uFF01");
            return ret;
        }
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        String strVal[] = astrParams[0].split(";");
        String strType[] = astrParams[1].split(";");
        if(astrSQL.equalsIgnoreCase(""))
            return ret;
        long curTime = System.currentTimeMillis();
        Connection con = ConnectionPool.getInstance().getConnection(astrDataSource);
        PreparedStatement ps = null;
        try
        {
            if(con == null)
            {
                _log.showLog("BaseDAO.update(string, string[]): \u65E0\u6548\u7684\u8FDE\u63A5\u3002");
                return false;
            }
            ps = con.prepareStatement(astrSQL);
            ps = DAOUtil.setParameters(strVal, strType, ps);
            if(ps == null)
            {
                con.close();
                return false;
            }
            if(ps.executeUpdate() >= 0)
            {
                con.commit();
                ret = true;
            } else
            {
                con.rollback();
            }
            ps.close();
            ps = null;
            con.close();
            con = null;
            _log.showLog("update(): Query Time = " + (System.currentTimeMillis() - curTime), "debug");
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            System.out.println("SQLException: param[0] = " + astrParams[0]);
            System.out.println("SQLException: param[1] = " + astrParams[1]);
            e.printStackTrace();
            return false;
        }
        catch(Exception e)
        {
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    return false;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.rollback();
                    con.close();
                }
                catch(SQLException ex)
                {
                    return false;
                }
                con = null;
            }
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            System.out.println("SQLException: param[0] = " + astrParams[0]);
            System.out.println("SQLException: param[1] = " + astrParams[1]);
            e.printStackTrace();
            return false;
        }
        finally
        {
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return false;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.rollback();
                    con.close();
                }
                catch(SQLException e)
                {
                    return false;
                }
                con = null;
            }
        }
        return ret;
    }

    public boolean update(String astrSQL, String astrParams[])
    {
        return update(astrSQL, astrParams, _defaultDataSource);
    }

    public boolean update(String astrSQL[], EntityBean aEntityBean, String astrDataSource)
    {
        boolean ret = false;
        String strLogHead = new String("BaseDAO.Update(String[], Entity):");
        if(astrSQL == null)
            return false;
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        long curTime = System.currentTimeMillis();
        Connection con = null;
        PreparedStatement ps = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDataSource);
            if(con == null)
            {
                _log.showLog("connection is null.");
                return false;
            }
            ps = con.prepareStatement(astrSQL[0]);
            ps = DAOUtil.setPs(con, astrSQL, aEntityBean);
            if(ps == null)
            {
                con.close();
                return false;
            }
            if(ps.executeUpdate() >= 0)
            {
                con.commit();
                ret = true;
            } else
            {
                con.rollback();
            }
            ps.close();
            ps = null;
            con.close();
            con = null;
            _log.showLog("update(): Query Time = " + (System.currentTimeMillis() - curTime), "debug");
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL[0] = " + astrSQL[0]);
            System.out.println("SQLException: Excute SQL[1] = " + astrSQL[1]);
            System.out.println("SQLException: Excute SQL[2] = " + astrSQL[2]);
            e.printStackTrace();
            return false;
        }
        catch(Exception e)
        {
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    return false;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.rollback();
                    con.close();
                }
                catch(SQLException ex)
                {
                    return false;
                }
                con = null;
            }
            System.out.println("SQLException: Excute SQL[0] = " + astrSQL[0]);
            System.out.println("SQLException: Excute SQL[1] = " + astrSQL[1]);
            System.out.println("SQLException: Excute SQL[2] = " + astrSQL[2]);
            e.printStackTrace();
            return false;
        }
        finally
        {
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return false;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.rollback();
                    con.close();
                }
                catch(SQLException e)
                {
                    return false;
                }
                con = null;
            }
        }
        return ret;
    }

    public boolean update(String astrSQL[], EntityBean aEntityBean)
    {
        return update(astrSQL, aEntityBean, _defaultDataSource);
    }

    public boolean batchUpdate(String astrSQL, List alsParams, String astrType, String astrDataSource)
    {
        boolean ret = false;
        boolean brtn = false;
        String strLogHead = new String("BaseDAO.batchUpdate(String, ArrayList): ");
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        if(astrSQL.length() < 10)
            return false;
        Connection con = null;
        PreparedStatement ps = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDataSource);
            con.setAutoCommit(false);
            ps = con.prepareStatement(astrSQL);
            ps = DAOUtil.setBatchSQL(astrSQL, alsParams, astrType, ps);
            if(ps == null)
            {
                con.close();
                _log.showLog(strLogHead + "\u65E0\u6CD5\u7ED9sql\u8BED\u53E5\u7684\u53C2\u6570\u8D4B\u503C\u3002");
                return false;
            }
            ps.execute();
            con.commit();
            brtn = true;
            ps.close();
            ps = null;
            con.close();
            con = null;
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            return false;
        }
        catch(Exception e)
        {
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    return false;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.rollback();
                    con.close();
                }
                catch(SQLException ex)
                {
                    return false;
                }
                con = null;
            }
            e.printStackTrace();
            return false;
        }
        finally
        {
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return false;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.rollback();
                    con.close();
                }
                catch(SQLException e)
                {
                    return false;
                }
                con = null;
            }
        }
        return brtn;
    }

    public boolean batchUpdate(String astrSQL, List alsParams, String astrType)
    {
        return batchUpdate(astrSQL, alsParams, astrType, _defaultDataSource);
    }

    public boolean batchUpdate(String astrSQL, EntityBean aEntityBean[], String strCols[], String astrDataSource)
    {
        boolean brtn = false;
        String strLogHead = new String("BaseDAO.batchUpdate(String, ArrayList): ");
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        if(astrSQL.length() < 10)
            return false;
        Connection con = null;
        PreparedStatement ps = null;
        String strType[] = new String[strCols.length];
        String strValue[] = new String[strCols.length];
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDataSource);
            con.setAutoCommit(false);
            ps = con.prepareStatement(astrSQL);
            int i = 0;
            for(int n = aEntityBean.length; i < n; i++)
            {
                for(int k = 0; k < strCols.length; k++)
                {
                    strType[k] = aEntityBean[i].getColType(strCols[k]);
                    if(strType[k] == null)
                        strType[k] = "string";
                    if(strType[k].equals(""))
                        strType[k] = "string";
                    strValue[k] = aEntityBean[i].getValue(strCols[k]);
                }

                ps = DAOUtil.setPSData(ps, strType, strValue);
                if(ps == null)
                {
                    con.close();
                    return false;
                }
                ps.addBatch();
            }

            if(ps.execute())
                con.commit();
            else
                con.rollback();
            ps.close();
            ps = null;
            con.close();
            con = null;
        }
        catch(SQLException e)
        {
            return false;
        }
        catch(Exception e)
        {
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    return false;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.rollback();
                    con.close();
                }
                catch(SQLException ex)
                {
                    return false;
                }
                con = null;
            }
            e.printStackTrace();
            return false;
        }
        finally
        {
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return false;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.rollback();
                    con.close();
                }
                catch(SQLException e)
                {
                    return false;
                }
                con = null;
            }
        }
        return brtn;
    }

    public boolean batchUpdate(String astrSQL, EntityBean aEntityBean[], String strCols[])
    {
        return batchUpdate(astrSQL, aEntityBean, strCols, _defaultDataSource);
    }

    public String multiUpdate(String aSQL[])
    {
        String strDBMS[] = new String[aSQL.length];
        for(int i = 0; i < aSQL.length; i++)
            strDBMS[i] = _defaultDataSource;

        return multiUpdate(aSQL, strDBMS);
    }

    public String multiUpdate(String aSQL[], String aDBMS[])
    {
        int len = aSQL.length;
        String strRtn = "0";
        Connection con[] = new Connection[len];
        PreparedStatement ps[] = new PreparedStatement[len];
        try
        {
            for(int i = 0; i < len; i++)
            {
                con[i] = ConnectionPool.getInstance().getConnection(aDBMS[i]);
                con[i].setAutoCommit(false);
                ps[i] = con[i].prepareStatement(aSQL[i]);
                if(ps[i].executeUpdate() >= 0)
                    continue;
                for(int j = 0; j < i; j++)
                    con[j].rollback();

                strRtn = "ERROR";
                break;
            }

            if(!strRtn.equals("ERROR"))
            {
                for(int k = 0; k < len; k++)
                {
                    con[k].commit();
                    ps[k].close();
                    con[k].close();
                }

            } else
            {
                strRtn = "DBMS_ERROR:600015:\u6570\u636E\u5E93\u63D0\u4EA4\u51FA\u9519\u3002";
                for(int k = 0; k < len; k++)
                {
                    ps[k].close();
                    con[k].close();
                }

            }
        }
        catch(SQLException ex)
        {
            System.out.println(ex.toString());
            for(int i = 0; i < len; i++)
                getSQLException(con[i], ex);

            strRtn = "DBMS_ERROR:600012:\u6570\u636E\u5E93\u9519\u8BEF\uFF1A" + ex.toString();
            return strRtn;
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
            for(int i = 0; i < len; i++)
                try
                {
                    con[i].close();
                }
                catch(SQLException e1)
                {
                    e1.printStackTrace();
                    con[i] = null;
                }

            return strRtn;
        }
        return strRtn;
    }

    public String multiUpdate(String aSQL[], EntityBean aEntityBean[][], String aCols[][])
    {
        String strDBMS[] = new String[aSQL.length];
        String strMain[] = new String[aSQL.length];
        for(int i = 0; i < aSQL.length; i++)
            strDBMS[i] = _defaultDataSource;

        return multiUpdate(aSQL, aEntityBean, aCols, strDBMS);
    }

    public String multiUpdate(String aSQL[], EntityBean aEntityBean[][], String aCols[][], String aDBMS[])
    {
        String strRtn = "0";
        int len = aSQL.length;
        String strLogHead = "BaseDAO.multiUpdate():";
        if(len < 1)
        {
            strRtn = "DBMS_ERROR 600001:\u6CA1\u6709\u6307\u5B9Asql\u8BED\u6CD5\u3002";
            return strRtn;
        }
        Connection con[] = (Connection[])null;
        PreparedStatement ps[] = (PreparedStatement[])null;
        try
        {
            for(int i = 0; i < len; i++)
            {
                con[i] = ConnectionPool.getInstance().getConnection(aDBMS[i]);
                con[i].setAutoCommit(false);
                ps[i] = con[i].prepareStatement(aSQL[i]);
                String strCol[] = aCols[i];
                String strVal[] = new String[strCol.length];
                String strType[] = new String[strCol.length];
                for(int j = 0; j < strCol.length; j++)
                {
                    if(strCol[j] == null)
                        strCol[j] = "string";
                    if(strCol[j].equals(""))
                        strCol[j] = "string";
                    strVal[j] = aEntityBean[i][j].getValue(strCol[j]);
                    strType[j] = aEntityBean[i][j].getColType(strCol[j]);
                }

                ps[i] = DAOUtil.setPSData(ps[i], strType, strVal);
                if(ps[i].executeUpdate() >= 0)
                    continue;
                for(int k = 0; k <= i; k++)
                    con[k].rollback();

                strRtn = "DBMS_ERROR 600003:\u4E8B\u52A1\u63D0\u4EA4\u5931\u8D25\u3002" + aSQL[i];
                break;
            }

            if(strRtn.indexOf("DBMS_ERROR") == -1)
            {
                for(int m = 0; m < len; m++)
                {
                    con[m].commit();
                    ps[m].close();
                    con[m].close();
                }

            } else
            {
                for(int k = 0; k < len; k++)
                {
                    ps[k].close();
                    con[k].close();
                }

            }
        }
        catch(SQLException ex)
        {
            System.out.println(ex.toString());
            for(int i = 0; i < len; i++)
                getSQLException(con[i], ex);

            strRtn = "DBMS_ERROR 600004:\u4E8B\u52A1\u63D0\u4EA4\u51FA\u73B0\u8FDD\u4F8B\u3002" + ex.toString();
            return strRtn;
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
            int j = 0;
            for(int n = con.length; j < n; j++)
                try
                {
                    con[j].close();
                    con[j] = null;
                }
                catch(SQLException e1)
                {
                    e1.printStackTrace();
                    con[j] = null;
                }

            return strRtn;
        }
        return strRtn;
    }

    public List queryWithTblCol(String astrSQL, String astrParam[], String astrDataSource)
    {
        List ret = FactoryUtil.newArray();
        if(astrSQL == null || astrSQL.length() == 0)
            return ret;
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDataSource);
            if(con == null)
            {
                _log.showLog("connection is null");
                List list = ret;
                return list;
            }
            ps = con.prepareStatement(astrSQL);
            ps = DAOUtil.setParameters(astrParam[0].split(";"), astrParam[1].split(";"), ps);
            if(ps == null)
            {
                con.close();
                con = null;
                List list1 = ret;
                return list1;
            }
            rs = ps.executeQuery();
            String strCollist[] = CommonFun.getCollistBySelect(astrSQL);
            ret = DAOUtil.setResult2List(rs, strCollist);
            rs.close();
            rs = null;
            ps.close();
            ps = null;
            con.close();
            con = null;
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            System.out.println("SQLException: param[0] = " + astrParam[0]);
            System.out.println("SQLException: param[1] = " + astrParam[1]);
            e.printStackTrace();
            List list2 = ret;
            return list2;
        }
        catch(Exception e)
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException ex)
                {
                    List list3 = getEmptyObjectByType(MUTIL_ROW);
                    return list3;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    List list4 = getEmptyObjectByType(MUTIL_ROW);
                    return list4;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException ex)
                {
                    List list5 = getEmptyObjectByType(MUTIL_ROW);
                    return list5;
                }
                con = null;
            }
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            System.out.println("SQLException: param[0] = " + astrParam[0]);
            System.out.println("SQLException: param[1] = " + astrParam[1]);
            e.printStackTrace();
            List list6 = ret;
            return list6;
        }
        finally
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                con = null;
            }
        }
        return ret;
    }

    public List queryWithTblCol(String astrSQL, String astrParam[])
    {
        return queryWithTblCol(astrSQL, astrParam, _defaultDataSource);
    }

    public List queryWithTblCol(String astrSQL, String astrDataSource)
    {
        if(astrSQL == null || astrSQL.length() == 0)
            return FactoryUtil.newArray();
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        List ret = FactoryUtil.newArray();
        long curTime = System.currentTimeMillis();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDataSource);
            if(con == null)
            {
                _log.showLog("connection is null");
                List list = FactoryUtil.newArray();
                return list;
            }
            con.setAutoCommit(true);
            ps = con.prepareStatement(astrSQL);
            rs = ps.executeQuery();
            String strCollist[] = CommonFun.getCollistBySelect(astrSQL);
            ret = DAOUtil.setResult2List(rs, strCollist);
            rs.close();
            rs = null;
            ps.close();
            ps = null;
            con.close();
            con = null;
            _log.showLog("queryWithTblCol(): Query Time = " + (System.currentTimeMillis() - curTime), "debug");
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            e.printStackTrace();
            List list1 = FactoryUtil.newArray();
            return list1;
        }
        catch(Exception e)
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException ex)
                {
                    List list2 = getEmptyObjectByType(MUTIL_ROW);
                    return list2;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    List list3 = getEmptyObjectByType(MUTIL_ROW);
                    return list3;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException ex)
                {
                    List list4 = getEmptyObjectByType(MUTIL_ROW);
                    return list4;
                }
                con = null;
            }
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            e.printStackTrace();
            List list5 = FactoryUtil.newArray();
            return list5;
        }
        finally
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException e)
                {
                    return getEmptyObjectByType(MUTIL_ROW);
                }
                con = null;
            }
        }
        return ret;
    }

    public List queryWithTblCol(String astrSQL)
    {
        return queryWithTblCol(astrSQL, _defaultDataSource);
    }

    public Map queryOneRowWithTblCol(String astrSQL, String astrParam[], String astrDataSource)
    {
        if(astrSQL == null || astrSQL.length() == 0)
            return null;
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        Map ret = FactoryUtil.newMap();
        long curTime = System.currentTimeMillis();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDataSource);
            if(con == null)
            {
                _log.showLog("connection is null");
                Map map = null;
                return map;
            }
            con.setAutoCommit(true);
            ps = con.prepareStatement(astrSQL);
            ps = DAOUtil.setParameters(astrParam[0].split(";"), astrParam[1].split(";"), ps);
            if(ps == null)
            {
                con.close();
                con = null;
                Map map1 = null;
                return map1;
            }
            rs = ps.executeQuery();
            String strCollist[] = CommonFun.getCollistBySelect(astrSQL);
            ret = DAOUtil.setResult2Map(rs, strCollist);
            rs.close();
            rs = null;
            ps.close();
            ps = null;
            con.close();
            con = null;
            _log.showLog("queryOneRowWithTblCol(): Query Time = " + (System.currentTimeMillis() - curTime), "debug");
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            System.out.println("SQLException: param[0] = " + astrParam[0]);
            System.out.println("SQLException: param[1] = " + astrParam[1]);
            e.printStackTrace();
            Map map2 = null;
            return map2;
        }
        catch(Exception e)
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException ex)
                {
                    Map map3 = null;
                    return map3;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    Map map4 = null;
                    return map4;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException ex)
                {
                    Map map5 = null;
                    return map5;
                }
                con = null;
            }
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            System.out.println("SQLException: param[0] = " + astrParam[0]);
            System.out.println("SQLException: param[1] = " + astrParam[1]);
            e.printStackTrace();
            Map map6 = null;
            return map6;
        }
        finally
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                con = null;
            }
        }
        return ret;
    }

    public Map queryOneRowWithTblCol(String astrSQL, String astrParam[])
    {
        return queryOneRowWithTblCol(astrSQL, astrParam, _defaultDataSource);
    }

    public Map queryOneRowWithTblCol(String astrSQL, String astrDataSource)
    {
        if(astrSQL == null || astrSQL.length() == 0)
            return null;
        if(astrDataSource == null || astrDataSource.equals(""))
            astrDataSource = _defaultDataSource;
        Map ret = FactoryUtil.newMap();
        long curTime = System.currentTimeMillis();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try
        {
            con = ConnectionPool.getInstance().getConnection(astrDataSource);
            if(con == null)
            {
                _log.showLog("connection is null");
                Map map = FactoryUtil.newMap();
                return map;
            }
            con.setAutoCommit(true);
            ps = con.prepareStatement(astrSQL);
            rs = ps.executeQuery();
            String strCollist[] = CommonFun.getCollistBySelect(astrSQL);
            ret = DAOUtil.setResult2Map(rs, strCollist);
            rs.close();
            rs = null;
            ps.close();
            ps = null;
            con.close();
            con = null;
            _log.showLog("queryOneRowWithTblCol(): Query Time = " + (System.currentTimeMillis() - curTime), "debug");
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            e.printStackTrace();
            Map map1 = null;
            return map1;
        }
        catch(Exception e)
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException ex)
                {
                    Map map2 = null;
                    return map2;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException ex)
                {
                    Map map3 = null;
                    return map3;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException ex)
                {
                    Map map4 = null;
                    return map4;
                }
                con = null;
            }
            System.out.println("SQLException: Excute SQL = " + astrSQL);
            e.printStackTrace();
            Map map5 = null;
            return map5;
        }
        finally
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                rs = null;
            }
            if(ps != null)
            {
                try
                {
                    ps.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                ps = null;
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch(SQLException e)
                {
                    return null;
                }
                con = null;
            }
        }
        return ret;
    }

    public Map queryOneRowWithTblCol(String astrSQL)
    {
        return queryOneRowWithTblCol(astrSQL, _defaultDataSource);
    }

    public static String ONE_ROW = "MAP";
    public static String MUTIL_ROW = "LIST";
    public static String COLUMN_NAME = "COLUMN";
    private static Log _log;
    public static String _defaultDataSource = "";

    static 
    {
        _log = Log.getInstance(com.sbdcpn.utils.db.BaseDAO.class);
    }
}